package cn.iocoder.yudao.module.system.controller.admin.mini.other.feedback;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.feedback.mapper.FeedbackMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.mysql.user.QdUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

//意见反馈
@RestController
@RequestMapping("/qd/discount")
public class FeedBackController {

    @Resource
    FeedbackMapper feedbackMapper;
    @Resource
    QdUserMapper qdUserMapper;

    @GetMapping("page")
    public CommonResult page(Integer pageNo,Integer pageSize){
        Page<FeedBackVo> result=new Page<>(pageNo,pageSize);
        List<FeedBackVo> list=new ArrayList<>();

        Page<FeedbackDO> page = feedbackMapper.selectPage(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<FeedbackDO>()
                .orderByDesc(BaseDO::getCreateTime)
        );
        for (FeedbackDO record : page.getRecords()) {
            UserDO user = qdUserMapper.selectById(record.getCreator());
            list.add(new FeedBackVo(record,user));
        }
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return CommonResult.success(result);
    }

    @DeleteMapping("delete")
    public CommonResult delete(Integer id){
        FeedbackDO dto = feedbackMapper.selectById(id);
        if (dto!=null)
            feedbackMapper.deleteById(id);
        return CommonResult.success("");
    }

}
@Data
class FeedBackVo{

    //内容
    private Integer id;
    //内容
    private String content;
    //图片s
    private List<String> urls;
    //内容
    private String openId;
    //内容
    private String phone;
    //昵称
    private String nickName;
    //是否开通vip(1是2否)
    private String isVip;
    //头像
    private String avatar;
    //创建时间
    private LocalDateTime createTime;

    public FeedBackVo() {
    }

    public FeedBackVo(FeedbackDO feedbackDO, UserDO user) {
        this.id=feedbackDO.getId();
        this.content = feedbackDO.getContent();
        this.urls = feedbackDO.getUrls();
        this.openId = user.getOpenId();
        this.phone = user.getPhone();
        this.nickName = user.getNickName();
        this.isVip = user.getIsVip()==1?"是":"否";
        this.avatar = user.getAvatar();
        this.createTime = feedbackDO.getCreateTime();
    }
}
